import { _decorator, Component, Node, Vec3, CameraComponent, find, Prefab } from 'cc';
import { EventMgr } from '../../../Framework/Scripts/Managers/EventMgr';
import { poolManager } from '../../../Framework/Scripts/Managers/poolManager';
import { ResMgr } from '../../../Framework/Scripts/Managers/ResMgr';
import { SoundMgr } from '../../../Framework/Scripts/Managers/SoundMgr';
import { TimerMgr } from '../../../Framework/Scripts/Managers/TimerMgr';
import { UIMgr } from '../../../Framework/Scripts/Managers/UIMgr';
import myUtil from '../../../Framework/Scripts/Utils/myUtil';
import { constant } from '../constant';
import { GameApp } from '../GameApp';
import { MonsterBloodBar } from './fight/monsterBloodBar';
const { ccclass, property } = _decorator;
const v3_tipsOffsetPos = new Vec3(-50, 175, 0);//血条距离小怪节点位置
const v3_monsterBloodOffsetPos = new Vec3(0, 500, 0);//血条距离小怪节点位置
const v3_monsterBloodScale = new Vec3(4, 2, 1);//小怪血条缩放大小



/** 
  周无邪出品
  本源码用于学习研究使用。
  作者联系方式：tuya7078（微信）。
 */
@ccclass('buildingCtrl')
export class buildingCtrl extends Component {
    @property({ type: Prefab, displayName: "爆炸动画" })
    private boomNode: Prefab = null
    @property({ type: Number, displayName: "生命" })
    private hp: number = 200

    private startHp: number = 0
    private nowHp: number = 0
    private isDie: boolean = false

    //  protected scriptBloodBar: MonsterBloodBar = null
    //   protected bloodBar: Node = null;
    start() {

    }
    onLoad() {

        this.startHp = this.hp
        this.startHp = this.startHp + (constant.buildData.hp.level * constant.buildData.hp.add)
        this.nowHp = this.startHp

        this.init()
    }
    /**
* 展示小怪血条
*  
* @param {*} scriptParent 
* @param {number} totalBlood
* @param {number} bloodBarType
* @param {Function} [callback=()=>{}]
* @param {Vec3} [offsetPos=MONSTER_BLOOD_OFFSET_POS]
* @param {Vec3} [scale=MONSTER_BLOOD_SCALE]
* @memberof uiManager
*/
    // protected showMonsterBloodBar(totalBlood: number, callback: Function = () => { }, offsetPos: Vec3 = v3_monsterBloodOffsetPos, scale: Vec3 = v3_monsterBloodScale) {
    //     if (!this.bloodBar) {
    //         this.bloodBar = poolManager.instance.getNode(ResMgr.Instance.getAsset("GUI", "fight/monsterBloodBar"), GameApp.Instance.bloodBar_root);



    //     }
    //     if (!this.scriptBloodBar) {
    //         this.scriptBloodBar = this.bloodBar.getComponent(MonsterBloodBar) as MonsterBloodBar;

    //     }
    //     let pos = GameApp.Instance.Camera.node.getComponent(CameraComponent).convertToUINode(this.node.worldPosition, find('Canvas') as Node) as Vec3;
    //     pos.add(offsetPos);
    //     this.bloodBar.setPosition(0, 0, 0)
    //     this.bloodBar.setScale(v3_monsterBloodScale)
    //     this.scriptBloodBar.show(this, totalBlood, offsetPos, scale, callback);





    // }
    init() {
        this.isDie = false



        // this.showMonsterBloodBar(this.nowHp)
    }
    public hit(num: number) {
        if (this.isDie) {
            return
        }
        this.nowHp -= num

        if (this.nowHp <= 0) {
            this.nowHp = 0
            this.isDie = true
            //   console.log("建筑倒塌");
            // this.scriptBloodBar.refreshBlood(-num)
            //游戏结束

            GameApp.Instance.isGameOver = true
            const n: Node = poolManager.instance.getNode(this.boomNode, this.node)
            n.setWorldPosition(this.node.getChildByName("boom").getWorldPosition())
            SoundMgr.Instance.playSound(ResMgr.Instance.getAsset("Sounds", "fireBallBig"))

            myUtil.playEffect(n, false, true, 0.2, true, 0, () => {
                this.node.getChildByName("castle").destroy()
                this.node.getChildByName("homeMinMap").destroy()

            })
            let t: number = TimerMgr.Instance.ScheduleOnce(() => {
                if (t) {
                    TimerMgr.Instance.UnSchedule(t)
                    t = null

                }
                EventMgr.Instance.Emit(constant.EVENT_NAME.subBuildHp, { nowHP: this.nowHp, startHP: this.startHp })

                UIMgr.Instance.IE_ShowUIView("GameOver")

            }, this, 1.5)


        } else {
            EventMgr.Instance.Emit(constant.EVENT_NAME.subBuildHp, { nowHP: this.nowHp, startHP: this.startHp })

            //   console.log("建筑血量："+this.nowHp);
            //   this.scriptBloodBar.refreshBlood(-num)

        }


    }
    update(deltaTime: number) {

    }
}


